# -*- coding: utf-8 -*-

'''eof
name:申请人年龄
code:Age
tableName:
columnName:
groups:个人通用
dependencies:EG_DSJ
type:常用指标
datasourceType:在线指标
description:
eof'''

import sys
import pandas as pd
import datetime
from dateutil.relativedelta import relativedelta

reload(sys)
sys.setdefaultencoding('utf-8')

'''
申请人年龄
    申请日期 : 当前日期 
    提取申请人身份证号(legalPersonIdCard)7-14位转化为日期格式，YEAR(申请日期 - 生日)，保留2位小数
'''

#result = {"ratingRequest":{"amount":"300000","serialNumber":"10027","address":"西安市碑林区和平路西三道巷","repaymentWay":"等额本息","tobaccoVerifyId":"197","companyName":"西安市碑林区创意产业发展有限公司","manualApproval":"null","accessToken":"yfTcvCqY5J3EgQDpK7dosI8h675","cellPhoneNum":"13551209162","bankCardId":"","timeLimit":"12","extendParams":"{address=西安市碑林区和平路西三道巷, tobaccoVerifyId=197, loanPurpose=扩大经营, creditReportSessionId=423, accessToken=yfTcvCqY5J3EgQDpK7dosI8h675, bankCardId=}","regNumber":null,"creditCode":"1234","sceneCode":"QKYD","legalPerson":"秦德强","legalPersonIdCard":"413026198812127330","loanPurpose":"扩大经营","id":"135","creditReportSessionId":"423","class":"class com.bbd.plutus.domain.RatingRequest"}}


def Age():
    null_type_list = ['', None, 'null', '/', 'Null']
    try:
        if ratingRequest['legalPersonIdCard'] not in null_type_list and \
                len(ratingRequest['legalPersonIdCard']) == 18:
            brith_date_str = ratingRequest['legalPersonIdCard'][6: 14]
            brith_date = datetime.datetime.strptime(brith_date_str, '%Y%m%d')
            r =  relativedelta(datetime.datetime.now(), brith_date)
            range_date = r.years + r.months/float(12) + r.days/float(365)
            return round(range_date , 4)
        else:
            return u'缺失值'
    except:
        return u'缺失值'

result = Age()